﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using MemoryGames.Games.WrongWord.WrongWordEntityTableAdapters;

namespace MemoryGames.Games.WrongWord
{
	/// <summary>
	/// Interaction logic for Maintenance.xaml
	/// </summary>
	public partial class Maintenance : UserControl, IGameControl
	{
		#region "Events"
		public event MainForm.MainFormRestartDelegate MainFormRestart;
		#endregion

		#region "MemberVariables"
		private WrongWordEntity wwe = new WrongWordEntity();
		private AnswerTableAdapter ata = new AnswerTableAdapter();
		private QuestionTableAdapter qta = new QuestionTableAdapter();
		private int? selectedQuestionId;
		private int? selectedAnswerId;
		#endregion

		#region "Constructors"
		public Maintenance()
		{
			InitializeComponent();
		}
		#endregion

		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			if (null != value)
			{
				return (int)value + 10;
			}
			else
			{
				return "bad color";
			}

		}

		#region "Methods"
		public bool Initialize()
		{
			Background = null;

			//One time
			FillQuestionList();

			//Add binding to listview
			lvQuestions.DataContext = wwe.Question;

			return true;
		}

		/// <summary>
		/// Fill Question list with question and summing of the answers
		/// </summary>
		private void FillQuestionList()
		{   //Dataset and dataadapters to the question and answer tables
			qta.Fill(wwe.Question);
			ata.Fill(wwe.Answer);

			////Get list of question (dataclass)
			//Question q;
			//foreach (WrongWordEntity.QuestionRow qr in wwe.Question)
			//{
			//    q = new Question()
			//    {
			//        QuestionId = qr.QuestionId,
			//        Description = qr.Question
			//    };
			//    foreach (WrongWordEntity.AnswerRow ar in wwe.Answer.Select(string.Format("QuestionId = {0}", qr.QuestionId)))
			//    {
			//        q.Answers += ar.Answer + "; ";
			//    }
			//    question.Add(q);
			//}
		}

		private void QuestionEdit(int questionId)
		{   //Cache selected question
			selectedQuestionId = questionId;

			WrongWordEntity.QuestionRow qr = wwe.Question.FindByQuestionId(questionId);

			//Fill question
			txtNumber.DataContext = qr;

			//Fill answers
			wwe.Answer.DefaultView.RowFilter = string.Format("QuestionId = {0}", questionId);
			gbQuestion.DataContext = wwe.Answer.DefaultView;
		}

		private void QuestionNew()
		{
			WrongWordEntity.QuestionRow qr = wwe.Question.NewQuestionRow();
			qr.Question = "";
			wwe.Question.AddQuestionRow(qr);
			QuestionEdit(qr.QuestionId);
		}

		private void AnswerNew()
		{
			WrongWordEntity.AnswerRow ar = wwe.Answer.NewAnswerRow();
			ar.Answer = "";
			ar.QuestionId = selectedQuestionId.Value;
			ar.CorrectAnswer = false;
			wwe.Answer.AddAnswerRow(ar);
		}

		#endregion

		#region "EventHandlers"
		private void lvQuestions_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			if (lvQuestions.SelectedItems.Count == 1)
			{
				QuestionEdit((((WrongWordEntity.QuestionRow)((System.Data.DataRowView)(lvQuestions).SelectedItems[0]).Row).QuestionId));
			}
		}

		private void btnQuestionAdd_Click(object sender, RoutedEventArgs e)
		{   QuestionNew();

		}
		#endregion

		private void button1_Click(object sender, RoutedEventArgs e)
		{

		}

		private void btnAnswerAdd_Click(object sender, RoutedEventArgs e)
		{
			AnswerNew();
		}

		private void Button_Click(object sender, RoutedEventArgs e)
		{
		}

		private void btnQuestionRemove_Click(object sender, RoutedEventArgs e)
		{
			if (lvQuestions.SelectedItems.Count == 1)
			{
				wwe.Question.FindByQuestionId(((WrongWordEntity.QuestionRow)((System.Data.DataRowView)(lvQuestions).SelectedItems[0]).Row).QuestionId).Delete();
			}
		}

		private void btnAnswerRemove_Click(object sender, RoutedEventArgs e)
		{
			wwe.Answer.FindByAnswerId(((WrongWordEntity.AnswerRow)((System.Data.DataRowView)(lvAnswers).SelectedItems[0]).Row).AnswerId).Delete();
		}

		private void btnSave_Click(object sender, RoutedEventArgs e)
		{
			qta.Update(wwe.Question);
			ata.Update(wwe.Answer);
			this.MainFormRestart.Invoke();
		}

		private void btnCancel_Click(object sender, RoutedEventArgs e)
		{
			this.MainFormRestart.Invoke();
		}
	}
}